<?php

function greengar_connect_twitter($id, $alias, $userid) {
    // Remove same fb account from all other users
    $userEntities = greengar_get_twitter($id, NULL);
    if ($userEntities & $userEntities[0]) {
        $userEntity = $userEntities[0];
        $userEntity->delete();
    } else {
        // No other user have same fb account :)
    }
    
    // Set or update account for current user
    $userEntities = greengar_get_twitter(NULL, $userid);

    if ($userEntities & $userEntities[0]) {
        $userEntity = $userEntities[0];
    } else {
        $userEntity = new TwitterAccount();
    }

    $userEntity->saveAccount($id, $alias);
    $userEntity->setOwner($userid);

    $returnEntity = $userEntity;

    


    return $returnEntity;
}

?>